import { createRouter, createWebHistory } from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/User/users.vue'
import roles from '../components/Role/roles.vue'
import rights from '../components/Role/rights.vue'
import goods from '../components/Goods/goods.vue'
import params from '../components/Goods/params.vue'
import categories from '../components/Goods/categories.vue'
import add from '../components/Goods/add.vue'
import orders from '../components/Order/orders.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login

  },
  {
    path:'/home',
    component: home,
    redirect: '/welcome',
    children:[
      {path:'/welcome',component:welcome},
      {path:'/users',component:users},
      {path:'/roles',component:roles},
      {path:'/rights',component:rights},
      {path:'/goods',component:goods},
      {path:'/params',component:params},
      {path:'/categories',component:categories},
      {path:'/add',component:add},
      {path:'/orders',component:orders},
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//设置一下路由守卫
//                    to 要访问的路径                from 从哪里来            next代表继续往下执行
router.beforeEach((to,from,next)=>{
  //登录，不拦截
  if( to.path === '/login' ) return next();
  //先获取token
  let token = sessionStorage.getItem('token');
  //如果没有token，那么进入到login
  if( !token ) return next('/login');
  //如果有，则放行
  next();
})

export default router
